home *** CD-ROM | disk | FTP | other *** search
/ Aminet 2 / Aminet AMIGA CDROM (1994)(Walnut Creek)[Feb 1994][W.O. 44790-1].iso / Aminet / mus / play / tracker_3_19.lzh / tracker / getopt.c < prev    next >
C/C++ Source or Header  |  1993-11-17  |  2KB  |  101 lines

  1. /* getopt.c */
  2.  
  3. /* $Id: getopt.c,v 1.4 1993/11/17 15:31:16 espie Exp espie $
  4.  * $Log: getopt.c,v $
  5.  * Revision 1.4  1993/11/17  15:31:16  espie
  6.  * *** empty log message ***
  7.  *
  8.  * Revision 1.3  1993/01/26  14:10:38  espie
  9.  * Fixed up stupdi end of file bug.
  10.  *
  11.  * Revision 1.2  1992/11/27  10:29:00  espie
  12.  * General cleanup
  13.  *
  14.  */
  15.  
  16. #include <stdio.h>
  17. #include <ctype.h>
  18.  
  19. #include "defs.h"
  20. #include "getopt.h"
  21.  
  22. int optind = 1;
  23. char *optarg = 0;
  24. static not_an_option = 0;
  25.  
  26. LOCAL int parse_option(argv, option)
  27. char *argv[];
  28. struct long_option *option;
  29.     {
  30.     optind++;
  31.     if (option->argn)
  32.         optarg = argv[optind++];
  33.     return option->abbrev;
  34.     }
  35.  
  36. int getopt(argc, argv, options)
  37. int argc;
  38. char *argv[];
  39. struct long_option *options;
  40.     {
  41.     if (not_an_option == optind)
  42.         return -1;
  43.     if (optind >= argc)
  44.         return -1;
  45.     if (argv[optind][0] == '-')
  46.         {
  47.         char *match = argv[optind]+1;
  48.         if (strlen(match) == 1)
  49.             {
  50.             if (match[0] == '-')
  51.                 {
  52.                 not_an_option = ++optind;
  53.                 return -1;
  54.                 }
  55.             while(options->fulltext)
  56.                 {
  57.                 if (options->abbrev == match[0])
  58.                     return parse_option(argv, options);
  59.                 else
  60.                     options++;
  61.                 }
  62.             return -1;
  63.             }
  64.         else
  65.             {
  66.             int max_match = 0;
  67.             struct long_option *best = 0;
  68.  
  69.             while (options->fulltext)
  70.                 {
  71.                 int i;
  72.                 for (i = 0; ; i++)
  73.                     {
  74.                     if (options->fulltext[i] == 0 && match[i] == 0)
  75.                         return parse_option(argv, options);
  76.                     if (match[i] == 0)
  77.                         {
  78.                         if (i > max_match)
  79.                             {
  80.                             max_match = i;
  81.                             best = options;
  82.                             }
  83.                         break;
  84.                         }
  85.                     if (tolower(options->fulltext[i]) != tolower(match[i]))
  86.                         break;
  87.                     }
  88.                 options++;
  89.                 }
  90.             if (max_match < 3)
  91.                 {
  92.                 fprintf(stderr, "Unrecognized option: %s\n", match);
  93.                 return -1;
  94.                 }
  95.             return parse_option(argv, best);
  96.             }
  97.         }
  98.     else
  99.         return -1;
  100.     }
  101.